'use stritc';

const crypto = require('crypto');

class UserConnector {
  constructor(ctx) {
    this.ctx = ctx;
  }
  async findById(id) {
    return await this.ctx.service.user.findById(id);
  }
  async create(post) {
    this.ctx.validate({
      username: {type: 'string', required: true},
      password: {type: 'string', required: true}
    }, {
      username: post.username,
      password: post.password
    });
    
    const password = crypto.createHmac('sha256', this.ctx.app.config.jwt.secret).update(post.password).digest('hex');

    const postData = {
      username: post.username,
      password: password
    }

    return await this.ctx.service.user.create(postData);
  }
}

module.exports = UserConnector;
